package cn.cheneyjoo.service.impl;

import cn.cheneyjoo.Model.Product;
import cn.cheneyjoo.mapper.GoodsMapper;
import cn.cheneyjoo.service.GoodsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.Map;
import java.util.Optional;

@Service
public class GoodServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 获取一个商品信息
     * @param id
     * @return
     */
    @Override
    public Product getOneProduct(Integer id) {
        return goodsMapper.selectById(id);
    }

    /**
     * 删除商品信息
     * @param id
     * @return
     */
    @Override
    public boolean deleteProduct(Integer id) {
        return goodsMapper.deleteById(id) > 0;
    }

    /**
     * 保存商品信息
     * @param product
     * @return
     */
    @Override
    public boolean saveProduct(Product product) {
        return (product.getId() > 0 ? goodsMapper.updateById(product) : goodsMapper.insert(product)) > 0;
    }

    /**
     * 分页查询
     *
     * @param pageIndex 页码
     * @param pageSize  每页条数
     * @param where     查询条件
     * @return
     */
    @Override
    public Page getProductsByPage(int pageIndex, int pageSize, Map<String, String> where) {
        Page<Product> productIPage = new Page<>(pageIndex, pageSize);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        if (where.containsKey("ProductName") && Optional.ofNullable(where.get("ProductName")).isPresent()) {
            queryWrapper.like("product_name", where.get("ProductName"));
        }
        if (where.containsKey("ProductType") && Optional.ofNullable(where.get("ProductType")).isPresent()) {
            queryWrapper.eq("product_type", Integer.parseInt(where.get("ProductType")));
        }
        if (where.containsKey("ProductMinPrice") && Optional.ofNullable(where.get("ProductMinPrice")).isPresent()) {
            queryWrapper.ge("product_price", new BigDecimal(where.get("ProductMinPrice")));
        }
        if (where.containsKey("ProductMaxPrice") && Optional.ofNullable(where.get("ProductMaxPrice")).isPresent()) {
            queryWrapper.le("product_price", new BigDecimal(where.get("ProductMaxPrice")));
        }
        if (where.containsKey("ProductMinStock") && Optional.ofNullable(where.get("ProductMinStock")).isPresent()) {
            queryWrapper.ge("product_stock", Integer.parseInt(where.get("ProductMinStock")));
        }
        if (where.containsKey("ProductMaxStock") && Optional.ofNullable(where.get("ProductMaxStock")).isPresent()) {
            queryWrapper.le("product_stock", Integer.parseInt(where.get("ProductMaxStock")));
        }
        if (where.containsKey("ProductStatus") && Optional.ofNullable(where.get("ProductStatus")).isPresent()) {
            queryWrapper.eq("product_status", Integer.parseInt(where.get("ProductStatus")));
        }
        productIPage = goodsMapper.selectPage(productIPage, queryWrapper);
        return productIPage;
    }
}
